package com.example.studentmanagersystem;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.studentmanagersystem.controller.StudentCon;
import com.example.studentmanagersystem.databases.DatabaseHelper;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {



    // 获取按钮，对按钮事件进行监听
    Button Login; // 登录按钮


    EditText account; // 账号
    EditText password; // 密码
    // 单选按钮
    RadioButton teacher; // 老师
    RadioButton student; // 学生

    DatabaseHelper mySqlHelper; // 学生数据库
    public static SQLiteDatabase db; // 数据库对象

    // 每个类的控制
    public StudentCon studentCon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化
        mySqlHelper = new DatabaseHelper(this, "student_man.bd", null,1);
        db=mySqlHelper.getWritableDatabase(); // 交给db

        studentCon = new StudentCon(db);

        /*
         * 登录的监听事件处理，判断当选框
         *
         * */
        Login=(Button) this.findViewById(R.id.login); // 登录按钮
        account=(EditText) this.findViewById(R.id.account); // 账号
        password=(EditText) this.findViewById(R.id.password); // 账号
        // 单选按钮
        teacher=(RadioButton) this.findViewById(R.id.TeacherButton); // 老师
        student=(RadioButton) this.findViewById(R.id.StudentButton); // 学生

        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 1 代表学生， 2 代表老师， 0 代表管理员
                if (account.getText().toString().equals("")){ // 判空
                    Toast toast=Toast.makeText(MainActivity.this, "请输入账号", Toast.LENGTH_SHORT);
                    toast.show();
                }else if (password.getText().toString().equals("")){
                    Toast toast=Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT);
                    toast.show();
                }else{
                    if (student.isChecked()){
                        int a = studentCon.LoginAccount(account.getText().toString(), password.getText().toString());
                        if (a==1){
                            Toast toast=Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT);
                            toast.show();
                            // 打开student_menu界面,跳转页面
                            // 将学号传递账号
                            Intent intent = new Intent(); // 初始化
                            intent.setClass(MainActivity.this, StudentManActivity.class);
                            intent.putExtra("account",account.getText().toString()); // 传参

                            startActivity(intent);

                        }else{
                            Toast toast=Toast.makeText(MainActivity.this, "账号或密码错误", Toast.LENGTH_SHORT);
                            toast.show();
                        }

                    }
                    if (teacher.isChecked()){
                        Toast toast=Toast.makeText(MainActivity.this, "选择了老师", Toast.LENGTH_SHORT);
                        toast.show();
                    }

                }
            }

        }); // 匿名对象

    }

}
